package com.lightheart.sphr.xiaobinggan.popupWindow;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lightheart.sphr.xiaobinggan.R;
import com.lightheart.sphr.xiaobinggan.base.BaseActivity;

import butterknife.BindView;

//https://juejin.im/post/6869689622671130632#heading-0
public class PopupWindowActivity extends BaseActivity {

    @BindView(R.id.tv)
    TextView tv;

    private MyPopWindow mPopWindow;
    private View mPopView;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_popup_window;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        mPopView = View.inflate(this, R.layout.pop_view, null);

//        if (mPopWindow !=null && mPopWindow.isShowing())
//            mPopWindow.dismiss();

        tv.setOnClickListener(view -> {
            showPop(mPopView);
        });
    }

    public void showPop(View view) {
        if (mPopWindow != null)
            mPopWindow.show();
        else {   //这里参数如果与默认值一致，无需再设置
            mPopWindow = new MyPopWindow.Builder(this)
                    .setLayoutView(mPopView)//设置弹窗布局
                    .setOutsideTouchable(true)//弹窗外是否可点击是否消失
                    .setBgTranslucent(true)//弹窗外是否半透明
                    .setHideStateBar(false)//是否隐藏状态栏
                    .setAnimStyle(R.style.pop_anim_style)//设置弹窗动画
                    .setHeight(400)//设置弹窗高度 单位px
                    .setWidth(ViewGroup.LayoutParams.MATCH_PARENT)//设置弹窗宽度，与屏幕同宽
                    .setOrientation(Gravity.BOTTOM)//弹窗出现的方向，Gravity.BOTTOM 底部弹出，Gravity.RIGHT 右侧弹出
                    .setPopDismissListener(new MyPopWindow.OnPopDismissListener()//设置弹窗关闭回调
                    {
                        @Override
                        public void onDismiss() {
                            Toast.makeText(PopupWindowActivity.this, "弹窗关闭了", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .build();
            mPopWindow.show();
            // mPopWindow.show(10,20); //设置弹窗距离屏幕的距离
        }
    }

    @Override
    protected void onDestroy() {
        if (mPopWindow != null)
            mPopWindow.destroy();//弹窗销毁，防止内存泄漏
        super.onDestroy();
    }

}